var showButton = function(){
	var button_html = ' <div class="btn-team">'+
		              ' <button class="btn btn-default" id="caculate" onclick="caculate()">一键计算</button>'+
		              ' <button class="btn btn-default" id="downExcelModel">导出数据</button></div>';
	$(".btn-team").remove();
	$("#div_content").prepend(button_html);
}

$("body").delegate("#downExcelModel","click",function(){
	var url = base+"/projectCenter/excelApi/exportExcelData/"+table_name+"/"+project_id;
	window.open(url,"_self");
	return false;
});

var table_name = "";

//生成设备概算信息
var caculate = function(){
	   layer.confirm('确认计算?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var load_index = layer.load(1, {shade: [0.1,'#fff'],scrollbar: false});
		   //var load_index = layer.msg('计算中', {icon: 16,shade: 0.01,scrollbar: false});
		   if(table_name != ""){
			   var param = {project_id:project_id,table_name:table_name}
			   $.ajax({
				   url:base+"/projectCenter/designcell/equipmntBudgetCaculate",
				   data:param,
				   success:function(data,status){
					   layer.close(load_index);
					    if(data.statusCode == 0){
							layer.msg(data.message, {
								icon : 1
							});
					    }else{
							layer.msg(data.message, {
								icon : 5
							});
					    }
					    initData(1);
				   }
			   }); 
		   }
	   });
}


//初始化设备概算
var initData = function(num){
	var url = base+"/projectCenter/designcell/equipmentBudget/";
	var param = {
			page_num:num,
			page_size:"10",
            project_id:project_id
	};
	$.ajax({
        url: url,
        data: param,
        success: function (data, textStatus, jqXHR) {
        	//debugger
        	if(data != null){
                var pageCount = data.data_list.pager.pageCount;
                table_name = data.table_name;
                var dataObj ={
                    "list":data.data_list.list,
                    "dataTool":{
                        "title":{
                        },
                        "titleCheckbox":false,
                        "btn":[{"event":"updateObj","name":"编辑"},
						       {"event":"delObj","name":"删除"}]
                    }
                };
                var title_list = data.title_list;
                var title = dataObj.dataTool.title;
                $.each(title_list,function(index,value){
                    var field_name = title_list[index].field_name;
                    var field_comment = title_list[index].field_comment;
                    if (field_name!="id"&& field_name!="project_id") {
                        title[field_name] = field_comment;
                    }
                });
                head_data_list = title; //将表头作为全局表里保存以便编辑操作
                title.operation = "操作";
                showPageTable('view', dataObj,num,pageCount,"initData");
        	}
        },
        error: function () {
        }
    });
	
}

//编辑自定义表单项
var updateObj = function(obj){
	var contentDiv = function(obj){
		var content;
		var data={
				"title":{
				},"text":{
				},"input":{
				},"input_init":{
				}
		};
	    var data_input = data.input;//弹出层表单类型
		delete head_data_list.operation; //删除操作字段
		data.title = head_data_list; //表头渲染到弹出层
		data.text = obj;
		//赋值操作
		$.each(data.title,function(index,value){
			data_input[index] = "text";
		});
		var getDiv =  dc_update.innerHTML;
		laytpl(getDiv).render(data, function(html){
			content = html;
		}); 
		return content;
	}
	layopen(contentDiv(obj));
}


var layopen = function(content){
	layer.open({
	  	title:'更新表单项'
		,content: content
    	,area: ['700px', '700px']
	    ,scrollbar: false
	  	,btn: ['确认', '取消']
		,yes: function(index,layero){ //确认
		  	var data = $(layero).find("form").serializeJSON();
		  	if(verifyCode(data)){
		  		commitUpdate(data);
		  	}
		}
});
}

//验证方法
var verifyCode = function(data){
	var flag = true;
/*	$.each(data,function(index,value){
		if(value == "" || value == null){
			var obj = $("input[name='"+index+"']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			flag = false;
			return false;
		}
	});*/
	return flag;
}

//提交更新方法
var commitUpdate = function(data){
	$.post(base+"/projectCenter/businessdata/updateDesignBudgetByTableName/"+table_name,data,
		function (data,status){
		if(data!=""&&data!=null){
			if(data.statusCode == 0){
				layer.msg('更新成功', {icon: 1,time:2000});
			}else{
				layer.msg(data.message, {icon: 2,time:2000});
			}
		}else{
			layer.msg('更新失败!', {icon: 2,time:2000});
		}
		initData(1);
		})
}

var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var id = obj.id;
			$.post(base+"/projectCenter/businessdata/delEquipmentBudgetDataById/"+table_name+"/"+id,{},
				function (data,status){
				if(data!=""&&data!=null){
					if(data.statusCode == 0){
						layer.msg('删除成功', {icon: 1,time:2000});
					}else{
						layer.msg(data.message, {icon: 2,time:2000});
					}
				}else{
					layer.msg('删除失败!', {icon: 2,time:2000});
				}
				initData(1);
				})
	   });
}

$(document).ready(function(){
	showButton();
	initData(1);
});